package com.moliku.insurance.common.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;

import java.util.Map;

/**
 * 查询条件规则
 *
 * @author Mr.Liu
 * @since 2020-02-01
 */
public class Condition {
    /**
     * 获取mybatis plus中的QueryWrapper
     *
     * @param entity 实体类
     * @param <T>    泛型
     * @return QueryWrapper
     */
    public static <T> QueryWrapper<T> getQueryWrapper(T entity) {
        return new QueryWrapper<>(entity);
    }

    /**
     * 获取mybatis plus中的QueryWrapper
     *
     * @param query 查询包装类
     * @param clazz 实体类
     * @param <T>   泛型
     * @return QueryWrapper
     */
    public static <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> query, Class<T> clazz) {
        //过滤非表字段
        query.remove("pageNo");
        query.remove("pageSize");
        query.remove("total");
        query.remove("sortField");
        query.remove("sortOrder");
        QueryWrapper<T> qw = new QueryWrapper<>();
        qw.setEntity(BeanUtils.instantiateClass(clazz));
        SqlKeyword.buildCondition(query, qw);
        return qw;
    }
}
